package com.sikaryofficial.system.manager;

import com.alibaba.fastjson2.JSON;
import com.sikaryofficial.common.core.utils.StringUtils;
import com.sikaryofficial.common.core.utils.ip.IpUtils;
import com.sikaryofficial.common.security.utils.SecurityUtils;
import com.sikaryofficial.system.api.model.ip.IpLocationVO;
import com.sikaryofficial.system.domain.PointReq;
import com.sikaryofficial.system.domain.SysMenuNew;
import com.sikaryofficial.system.domain.SysMenuNewSaveReq;
import com.sikaryofficial.system.domain.dto.CountryGeoDTO;
import com.sikaryofficial.system.domain.dto.SysMenuNewResp;
import com.sikaryofficial.system.domain.mapping.SysMenuNewMapping;
import com.sikaryofficial.system.service.IGadmCountryGeoService;
import com.sikaryofficial.system.service.ISysMenuNewService;
import com.sikaryofficial.system.service.impl.Ip2LocationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author : wuweihong
 * @desc : TODO  请填写你的功能描述
 * @date : 2025-08-11
 */

@Slf4j
@Service
public class SysMenuNewManager {

	@Autowired
	private ISysMenuNewService sysMenuNewService;

	@Autowired
	private IGadmCountryGeoService gadmCountryGeoService;

	public void saveOrUpdate(SysMenuNewSaveReq saveReq) {
		SysMenuNew sysMenuNew = SysMenuNewMapping.INSTANCE.covertToReq(saveReq);
		if (Objects.nonNull(sysMenuNew.getMenuId())) {
			sysMenuNew.setUpdateBy(SecurityUtils.getUserId());
			sysMenuNew.setUpdateTime(new Date());
		}else {
			sysMenuNew.setCreateBy(SecurityUtils.getUserId());
			sysMenuNew.setCreateTime(new Date());
			sysMenuNew.setParentId(Objects.isNull(sysMenuNew.getParentId()) ? 0: sysMenuNew.getParentId());
		}
		sysMenuNewService.saveOrUpdate(sysMenuNew);
	}

	public List<SysMenuNewResp> getChildren(Long parentId) {
		return SysMenuNewMapping.INSTANCE.covertToListResp(sysMenuNewService.selectMenuList(parentId));
	}

	public List<SysMenuNewResp> getMenuByCountry(PointReq pointReq) {
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		assert  attributes != null;
		List<SysMenuNew> sysMenuNews = Collections.emptyList();
		if (StringUtils.isEmpty(pointReq.getLatitude()) ||  StringUtils.isEmpty(pointReq.getLongitude())) {
			HttpServletRequest request = attributes.getRequest();
			String ip = IpUtils.getIpAddr(request);
			IpLocationVO ipLocationVO = gadmCountryGeoService.getGeographyInfo(ip);
			sysMenuNews = sysMenuNewService.selectMenuList(ipLocationVO.getCountryShort());
		}else {
			List<CountryGeoDTO> countryGeoDTOS = gadmCountryGeoService.findCountryByPoint(pointReq.getLatitude(), pointReq.getLongitude());
			if (CollectionUtils.isNotEmpty(countryGeoDTOS)) {
				sysMenuNews = sysMenuNewService.selectMenuListByIsoCode(countryGeoDTOS.get(0).getIsoCountryCode());
			}
		}
		return SysMenuNewMapping.INSTANCE.covertToListResp(sysMenuNews);
	}

	public List<SysMenuNewResp> getAllMenu(String country) {
		List<SysMenuNew> sysMenuNews = sysMenuNewService.selectMenuListByCountryName(country);
		return SysMenuNewMapping.INSTANCE.covertToListResp(sysMenuNews);
	}

}
